package com.ruoyi.web.controller.dictionary;

import java.text.SimpleDateFormat;
import java.util.List;

import com.ruoyi.domain.SysMatClassificationType;
import com.ruoyi.domain.SysMatType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.ISysMatClassificationTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.SysMatDataType;
import com.ruoyi.service.ISysMatDataTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 材料数据Controller
 * 
 * @author ruoyi
 * @date 2020-07-03
 */
@Controller
@RequestMapping("/ruoyi/data")
public class SysMatDataTypeController extends BaseController
{
    private String prefix = "ruoyi/data";

    @Autowired
    private ISysMatDataTypeService sysMatDataTypeService;
    @Autowired
    private ISysMatClassificationTypeService sysMatClassificationTypeService;

    @RequiresPermissions("ruoyi:data:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询材料数据列表
     */
    @RequiresPermissions("ruoyi:data:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(String matClassificationType)
    {
        startPage();
        List<SysMatDataType> list = sysMatDataTypeService.selectSysMatDataTypeLists(matClassificationType);
        return getDataTable(list);
    }

    /**
     * 导出材料数据列表
     */
    @RequiresPermissions("ruoyi:data:export")
    @Log(title = "材料数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysMatDataType sysMatDataType)
    {
        List<SysMatDataType> list = sysMatDataTypeService.selectSysMatDataTypeList(sysMatDataType);
        ExcelUtil<SysMatDataType> util = new ExcelUtil<SysMatDataType>(SysMatDataType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 新增材料数据
     */
    @GetMapping("/add/{matClassificationType}")
    public String add(@PathVariable("matClassificationType") String matClassificationType, ModelMap mmap)
    {
        mmap.put("matClassificationType", matClassificationType);
        return prefix + "/add";
    }

    /**
     * 新增保存材料数据
     */
    @RequiresPermissions("ruoyi:data:add")
    @Log(title = "材料数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMatDataType sysMatDataType)
    {
        //获取创建者
        sysMatDataType.setMatDataCreateBy(ShiroUtils.getLoginName());
        //获取创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        return toAjax(sysMatDataTypeService.insertSysMatDataType(sysMatDataType));
    }

    /**
     * 修改材料数据
     */
    @GetMapping("/edit/{matDataId}")
    public String edit(@PathVariable("matDataId") Integer matDataId, ModelMap mmap)
    {
        SysMatDataType sysMatDataType = sysMatDataTypeService.selectSysMatDataTypeById(matDataId);
        mmap.put("sysMatDataType", sysMatDataType);
        return prefix + "/edit";
    }

    /**
     * 修改保存材料数据
     */
    @RequiresPermissions("ruoyi:data:edit")
    @Log(title = "材料数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMatDataType sysMatDataType)
    {
        //获取创建者
        sysMatDataType.setMatDataCreateBy(ShiroUtils.getLoginName());
        //获取创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        return toAjax(sysMatDataTypeService.updateSysMatDataType(sysMatDataType));
    }

    /**
     * 删除材料数据
     */
    @RequiresPermissions("ruoyi:data:remove")
    @Log(title = "材料数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysMatDataTypeService.deleteSysMatDataTypeByIds(ids));
    }

    /**
     * 查询字典详细
     */
    @RequiresPermissions("ruoyi:data:details")
    @GetMapping("/details/{matClassificationType}")
    public String detail(@PathVariable("matClassificationType") String matClassificationType, ModelMap mmap) {
        mmap.put("matClassificationType",matClassificationType);
        return prefix + "/type";
    }
}
